import 'package:get/get.dart';
import 'package:mtf/app/data/NewsEntity.dart';
import 'package:mtf/app/net/HttpManger.dart';
import 'package:mtf/app/routes/app_pages.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../data/ClassifyBeanEntity.dart';
import '../../utils/AppUtils.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class evaluateRecordController extends GetxController{
  var page = 1;
  RxList<TypeList> typeList=<TypeList>[].obs;
  RxList<ListNewsElement> articles=<ListNewsElement>[].obs;
  RefreshController refreshController = RefreshController(initialRefresh: true);
  RxList<ListNewsElement> evaluateList = <ListNewsElement>[].obs;

  void clickRecordDetail(ListNewsElement item){
    Get.toNamed(Routes.EVALUATE_DETAIL,arguments: {
      "title": item.title,
      "time": AppUtils.dateToString(item.updatedAt),
      "content": item.content,
      "cover": item.imgHead
    });
  }

  @override
  void onInit() {
    super.onInit();
    reqNewsHome();
  }

  void reqNewsHome() async{
    var classifyId=Get.arguments["classifyId"];
    var resp= await HttpManager.reqClassifyEvaluateHome(classifyId);
    if(resp.isSuccessData()){
      typeList.assignAll(resp.data!.typeList!);
      articles.assignAll(resp.data!.articles!);

    }
  }

  void reqNewsList(int tabIndex,RefreshController refreshController) async {
    var resp= await HttpManager.reqNewsList(page,typeList[tabIndex].typeId.toString());
    if(resp.isSuccess()&&resp.data!=null){
      if(page==1){
        evaluateList.assignAll(resp.data!.list);
        refreshController.refreshCompleted();
      }else{
        evaluateList.addAll(resp.data!.list);
        if(resp.data!.list.isEmpty){
          refreshController.loadNoData();
        }else{
          refreshController.loadComplete();
        }
      }}else{
      if(page==1){
        evaluateList.assignAll([]);
      }
      refreshController.refreshCompleted();
      refreshController.loadComplete();
    }
  }
}
